package com.gem.web;

import com.gem.entity.Commodity;
import com.gem.entity.OrderDetails;
import com.gem.service.CommodityService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author zhang
 * @date 2020/10/4 15:31
 */
@Controller
//窄化处理
@RequestMapping("/permission/commodity")
@SessionAttributes(value = {"cart"})
public class CommodityController {

    @Autowired
    CommodityService commodityService;

    /**
     *展示所有
     * @param pageNow 当前页码
     * @param model
     * @return
     */
    @RequestMapping("/allType")
    public String allType(String pageNow, Model model){
        int pageNowNum;
        if (pageNow == null){
            pageNowNum = 1;
        }else {
            pageNowNum = Integer.parseInt(pageNow);
        }
        PageInfo<Commodity> pageInfo = commodityService.findCommoditysByPagesAndType(pageNowNum,5,"每日特价");
        List<String> allType = commodityService.findAllType();
        model.addAttribute("allType",allType);
        model.addAttribute("pageInfo",pageInfo);
        System.out.println(pageInfo.getList());
        return "home";
    }

    /**
     *通过类型查找
     * @param type  商品类型
     * @param name  商品名称
     * @param model
     * @return
     */
    @RequestMapping("/byType")
    public String byType(String type,String name,Model model){
        List<Commodity> commodities = commodityService.findCommoditiesByNameAndType(name,type);
        model.addAttribute("commodities",commodities);
        model.addAttribute("name",name);
        model.addAttribute("type",type);
        return "showCommodities";
    }


    /**
     * 跳转到商品详情页面
     * @param id 商品id
     * @param model
     * @return
     */
    @RequestMapping("/toCommodityDetail")
    public String toCommodityDetail(int id,Model model){
        Commodity commodity = commodityService.findCommodityById(id);
        System.out.println(commodity);
        model.addAttribute("commodity",commodity);
        return "commoditiesDetails";
    }


    /**
     * 添加购物车
     * @param commodity_id 商品id
     * @param num  数量
     * @param model
     * @return
     */
    @RequestMapping("/addCart")
    @ResponseBody
    public String addCart(int commodity_id,int num,Model model){
        Commodity commodity = commodityService.findCommodityById(commodity_id);

        //购物车映射成订单明细
        OrderDetails cart = new OrderDetails(null,num,num * commodity.getPrice(),commodity.getPrice(),null,commodity);
        List<OrderDetails> list = (List<OrderDetails>) model.asMap().get("cart");

        if (list == null || list.size() == 0){
            //session中没有
            list = new ArrayList<>();
        }

        boolean falg = true;
        //判单集合中是否存在该商品信息
        for (OrderDetails orderDetails : list) {
            if (orderDetails.getCommodity().getId().equals(cart.getCommodity().getId())){
                //表示集合中已经有该商品  数量相加就可以了
                orderDetails.setNum(orderDetails.getNum()+cart.getNum());
                orderDetails.setSubtotal(orderDetails.getPrice() * orderDetails.getNum());
                falg = false;
                break;
            }
        }

        if (falg){
            //表示不满足循环中的if条件，将cart添加到集合
            list.add(cart);
        }

        model.addAttribute("cart",list);

        return "1";
    }

    /**
     * 跳转到我的购物车
     * @return
     */
    @RequestMapping("/toMyCart")
    public String toMyCart(){
        return "myCart";
    }


    /**
     * 删除购物车指定商品
     * @param id  商品id
     * @param model
     * @return
     */
    @RequestMapping("/deleteCart")
    public String deleteCart(int id, Model model){
        List<OrderDetails> list = (List<OrderDetails>) model.asMap().get("cart");
        Iterator<OrderDetails> iterator = list.iterator();
        while (iterator.hasNext()){
            //判断是不是要删除的商品
            if (id == iterator.next().getCommodity().getId()){
                iterator.remove();
            }
        }

        model.addAttribute("cart",list);
        return "myCart";
    }


    /**
     * 跳转到购买商品页面
     * @param id
     * @param num
     * @param model
     * @return
     */
    @RequestMapping("/toBuy")
    public String toBuy(int id,int num,Model model){
        Commodity commodity = commodityService.findCommodityById(id);
        model.addAttribute("commodity",commodity);
        model.addAttribute("num",num);
        model.addAttribute("subtotal",num * commodity.getPrice());
        return "payOrder";
    }

}
